package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.SysDoctor;
import com.mars.module.admin.entity.SysVisitRecords;
import com.mars.module.admin.mapper.SysDoctorMapper;
import com.mars.module.admin.service.ISysDictDataService;
import com.mars.module.system.entity.SysUser;
import com.mars.module.system.entity.SysUserRole;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysDoctorRecordsRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysDoctorRecordsMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysDoctorRecords;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysDoctorRecordsService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 医生出诊记录业务层处理
 *
 * @author caiJin
 * @date 2024-05-13
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysDoctorRecordsServiceImpl implements ISysDoctorRecordsService {

    private final SysDoctorRecordsMapper baseMapper;
    private final ISysDictDataService iSysDictDataService;
    private final SysDoctorMapper doctorMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysDoctorRecords add(SysDoctorRecordsRequest request) {
        SysDoctorRecords entity = SysDoctorRecords.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysDoctorRecordsRequest request) {
        SysDoctorRecords entity = SysDoctorRecords.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysDoctorRecords getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysDoctorRecords> pageList(SysDoctorRecordsRequest request) {
        Page<SysDoctorRecords> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysDoctorRecords> query = this.buildWrapper(request);
        IPage<SysDoctorRecords> pageRecord = baseMapper.selectPage(page, query);
        List<SysDoctorRecords> records = pageRecord.getRecords();
        List<SysDoctorRecords> recordsNew = new ArrayList<>();
        for (SysDoctorRecords e : records) {
            SysDoctor doctor = doctorMapper.selectById(e.getDoctorId());
            e.setDoctorName(doctor == null ? "" : doctor.getName());
            if ("1".equals(e.getStatus())) {
                e.setStatusName("正常");
            } else if ("0".equals(e.getStatus())) {
                e.setStatusName("已取消");
            }
            recordsNew.add(e);
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysDoctorRecords> list(SysDoctorRecordsRequest request) {
        LambdaQueryWrapper<SysDoctorRecords> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysDoctorRecords> buildWrapper(SysDoctorRecordsRequest param) {
        LambdaQueryWrapper<SysDoctorRecords> query = new LambdaQueryWrapper<>();
        if (param.getVisitId() != null) {
            query.like(SysDoctorRecords::getVisitId, param.getVisitId());
        }
        if (StringUtils.isNotBlank(param.getStatus())) {
            query.like(SysDoctorRecords::getStatus, param.getStatus());
        }
        return query;
    }

}
